#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

export PATH=$TOP_DIR/cross/bin:$PATH

# export MAKE_OPT="-j 2"

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -d opencore-amr-0.1.3 ] ; then
		wget -O opencore-amr-0.1.3.tar.gz http://sourceforge.net/projects/opencore-amr/files/opencore-amr/opencore-amr-0.1.3.tar.gz/download
		tar xzf opencore-amr-0.1.3.tar.gz
	fi
	if [ ! -d vo-amrwbenc-0.1.3 ] ; then
		wget -O vo-amrwbenc-0.1.3.tar.gz http://sourceforge.net/projects/opencore-amr/files/vo-amrwbenc/vo-amrwbenc-0.1.3.tar.gz/download
		tar xzf vo-amrwbenc-0.1.3.tar.gz
	fi
	if [ ! -d vo-aacenc-0.1.3 ] ; then
		wget -O vo-aacenc-0.1.3.tar.gz http://sourceforge.net/projects/opencore-amr/files/vo-aacenc/vo-aacenc-0.1.3.tar.gz/download
		tar xzf vo-aacenc-0.1.3.tar.gz
	fi
	cd ..
fi


if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
	fi
	rm -rf $BIN_DIR/opencoreamr
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_opencoreamr bd_voamrwbenc bd_voaacenc

	mkdir bd_opencoreamr
	cd bd_opencoreamr
	$SRC_DIR/opencore-amr-0.1.3/configure $HOST_FLAG --prefix=$BIN_DIR/opencoreamr \
		--disable-shared --enable-static
	make $MAKE_OPT
	make install
	cd ..

	mkdir bd_voamrwbenc
	cd bd_voamrwbenc
	$SRC_DIR/vo-amrwbenc-0.1.3/configure $HOST_FLAG --prefix=$BIN_DIR/opencoreamr \
		--disable-shared --enable-static
	make $MAKE_OPT
	make install
	cd ..

	mkdir bd_voaacenc
	cd bd_voaacenc
	$SRC_DIR/vo-aacenc-0.1.3/configure $HOST_FLAG --prefix=$BIN_DIR/opencoreamr \
		--disable-shared --enable-static
	make $MAKE_OPT
	make install
	cd ..

	cd ..
fi

